package com.example.riceexambackend.controller;

import com.example.riceexambackend.common.errorcode.ControllerErrorCodeConstants;
import com.example.riceexambackend.common.exception.ControllerException;
import com.example.riceexambackend.common.pojo.CommonResult;
import com.example.riceexambackend.common.utils.JacksonUtil;
import com.example.riceexambackend.controller.param.questiontype.QuestionTypeAddParam;
import com.example.riceexambackend.controller.param.questiontype.QuestionTypeDeleteParam;
import com.example.riceexambackend.controller.param.questiontype.QuestionTypeUpdateParam;
import com.example.riceexambackend.controller.result.questiontype.QuestionTypeAddResult;
import com.example.riceexambackend.controller.result.questiontype.QuestionTypeDeleteResult;
import com.example.riceexambackend.controller.result.questiontype.QuestionTypeSelectResult;
import com.example.riceexambackend.controller.result.questiontype.QuestionTypeUpdateResult;
import com.example.riceexambackend.service.QuestionTypeService;
import com.example.riceexambackend.service.dto.questiontype.QuestionTypeAddDTO;
import com.example.riceexambackend.service.dto.questiontype.QuestionTypeDeleteDTO;
import com.example.riceexambackend.service.dto.questiontype.QuestionTypeSelectDTO;
import com.example.riceexambackend.service.dto.questiontype.QuestionTypeUpdateDTO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/question_type")
public class QuestionTypeController {
    @Resource
    private QuestionTypeService questionTypeService;
    private static final Logger logger = LoggerFactory.getLogger(QuestionTypeController.class);

    @GetMapping("/select")
    public CommonResult<List<QuestionTypeSelectResult>> selectAllQuestionType() {
        // 日志打印
        logger.info("QuestionTypeController selectAllQuestionType");
        // 调用查询全部题型业务逻辑
        List<QuestionTypeSelectDTO> questionTypeSelectDTOList = questionTypeService.selectAllQuestionType();
        List<QuestionTypeSelectResult> resultList = convertToQuestionTypeSelectResultList(questionTypeSelectDTOList);
        return CommonResult.success(resultList);
    }

    @PostMapping("/delete")
    public CommonResult<QuestionTypeDeleteResult> deleteQuestionType(@Validated @RequestBody QuestionTypeDeleteParam param) {
        // 日志打印
        logger.info("QuestionTypeController deleteQuestionType:{}", JacksonUtil.writeValueAsString(param));
        // 调用删除题型业务逻辑
        QuestionTypeDeleteDTO questionTypeDeleteDTO = questionTypeService.deleteQuestionType(param);
        QuestionTypeDeleteResult result = convertToQuestionTypeDeleteResult(questionTypeDeleteDTO);
        return CommonResult.success(result);
    }

    private QuestionTypeDeleteResult convertToQuestionTypeDeleteResult(QuestionTypeDeleteDTO questionTypeDeleteDTO) {
        if (questionTypeDeleteDTO == null ||
            questionTypeDeleteDTO.getQuestionTypeId() <= 0) {
            throw new ControllerException(ControllerErrorCodeConstants.QUESTION_TYPE_DELETE_ERROR);
        }
        QuestionTypeDeleteResult questionTypeDeleteResult = new QuestionTypeDeleteResult();
        questionTypeDeleteResult.setQuestionTypeId(questionTypeDeleteDTO.getQuestionTypeId());
        return questionTypeDeleteResult;
    }

    private List<QuestionTypeSelectResult> convertToQuestionTypeSelectResultList(List<QuestionTypeSelectDTO> questionTypeSelectDTOList) {
        if (questionTypeSelectDTOList == null) {
            throw new ControllerException(ControllerErrorCodeConstants.QUESTION_TYPE_SELECT_ALL_ERROR);
        }
        List<QuestionTypeSelectResult> questionTypeSelectResultList = new ArrayList<>();
        for (QuestionTypeSelectDTO questionTypeSelectDTO : questionTypeSelectDTOList) {
            QuestionTypeSelectResult questionTypeSelectResult = new QuestionTypeSelectResult();
            questionTypeSelectResult.setId(questionTypeSelectDTO.getId());
            questionTypeSelectResult.setQuestionName(questionTypeSelectDTO.getQuestionName());
            questionTypeSelectResult.setQuestionScore(questionTypeSelectDTO.getQuestionScore());
            questionTypeSelectResultList.add(questionTypeSelectResult);
        }
        return questionTypeSelectResultList;
    }

    @PostMapping("/update")
    public CommonResult<QuestionTypeUpdateResult> updateQuestionType(@Validated @RequestBody QuestionTypeUpdateParam param) {
        // 日志打印
        logger.info("QuestionTypeController updateQuestionType:{}", JacksonUtil.writeValueAsString(param));
        // 调用修改题型业务逻辑
        QuestionTypeUpdateDTO questionTypeUpdateDTO = questionTypeService.updateQuestionType(param);
        QuestionTypeUpdateResult result = convertToQuestionTypeUpdateResult(questionTypeUpdateDTO);
        return CommonResult.success(result);
    }

    private QuestionTypeUpdateResult convertToQuestionTypeUpdateResult(QuestionTypeUpdateDTO questionTypeUpdateDTO) {
        if (questionTypeUpdateDTO == null ||
        questionTypeUpdateDTO.getQuestionTypeId() <= 0) {
            throw new ControllerException(ControllerErrorCodeConstants.QUESTION_TYPE_UPDATE_ERROR);
        }
        QuestionTypeUpdateResult questionTypeUpdateResult = new QuestionTypeUpdateResult();
        questionTypeUpdateResult.setQuestionTypeId(questionTypeUpdateDTO.getQuestionTypeId());
        return questionTypeUpdateResult;
    }

    @PostMapping("/add")
    public CommonResult<QuestionTypeAddResult> addQuestionType(@Validated @RequestBody QuestionTypeAddParam param) {
        // 日志打印
        logger.info("QuestionTypeController addQuestionType:{}", JacksonUtil.writeValueAsString(param));
        // 调用用户注册业务逻辑
        QuestionTypeAddDTO questionTypeAddDTO = questionTypeService.insertQuestionType(param);
        QuestionTypeAddResult result = convertToQuestionTypeAddResult(questionTypeAddDTO);
        return CommonResult.success(result);
    }

    private QuestionTypeAddResult convertToQuestionTypeAddResult(QuestionTypeAddDTO questionTypeAddDTO) {
        if (questionTypeAddDTO == null
            || questionTypeAddDTO.getQuestionTypeId() <= 0) {
            throw new ControllerException(ControllerErrorCodeConstants.QUESTION_TYPE_ADD_ERROR);
        }
        QuestionTypeAddResult questionTypeAddResult = new QuestionTypeAddResult();
        questionTypeAddResult.setQuestionTypeId(questionTypeAddDTO.getQuestionTypeId());
        return questionTypeAddResult;
    }
}
